/**
 * Created by Administrator on 2017/1/16.
 */
import { Component } from '@angular/core';
import { Router, Params } from '@angular/router';
import { BindPublicNumberService } from '../../service/bindPublicNumber.service'

declare let $:any;


@Component({
    selector: 'customService',
    templateUrl: '../../../views/bind_public_number/publicNumber.html',
    styleUrls: ['../../../css/bind_public_number/bindPublicNumber.css'],
    providers: [ BindPublicNumberService ]
})
export class bindPublicNumberComponent {
    public api_key: any;
    public app_id: any;
    public app_secret: any;
    public encoding_aes_key: any;
    public mch_id: any;
    public title: any;
    public token: any;
    public url: any;
    public phone: any = $.cookie("phone");
    public has_mp:any = $.cookie("has_mp");

    constructor (
        private bindPN : BindPublicNumberService,
        private router : Router
    ){
        this.load();
    }

    public load(){
        let self : any = this;
        self.id = $.cookie("id");
        let data : any = {
            member_id: self.id
        };
        self.bindPN.loadPublicNumber({
            data: data,
            success: function (result:any) {
                self.api_key = result.api_key;
                self.app_id = result.app_id;
                self.app_secret = result.app_secret;
                self.encoding_aes_key = result.encoding_aes_key;
                self.mch_id = result.mch_id;
                self.title = result.title;
                self.token = result.token;
                self.url = result.url;
            }
        });
    }
    // 绑定公众号
    public bPN(){
         this.bindPublicN()
    }
    private bindPublicN(){
        let self : any = this;
        self.id = $.cookie("id");
        let data : any = {
            api_key: self.api_key,
            app_id: self.app_id,
            app_secret: self.app_secret,
            encoding_aes_key: self.encoding_aes_key,
            mch_id: self.mch_id,
            title: self.title,
            token: self.token,
            url: self.url,
            member_id: self.id
        };
        self.bindPN.bindPublicNumber({
            data: data,
            success: function (result:any){
                console.log(result);
                if(result.error === 0){
                    self.has_mp = 1;
                    $.cookie("has_mp",self.has_mp);
                    // $.cookie("app_id",self.app_id);
                    alert("绑定成功");
                    self.router.navigate(['/home']);
                }else {
                    alert(result.msg);
                }
            }
        });
    }
}
